<?php
/**
 * This will create an object containing information about 1 article.
 * The constructor expects an array. This is makes it easy to pass information
 * out of the database on.
 *
 * setSummary accepts a true or false value. Setting it to true will make let
 * other functions know we want a summary of the article. In this case, the
 * summary is the first line.
 *
 * To get the first line, getFirstLine is called. It looks for the first period
 * in the article. If it can't find any, it will instead cut off the text at
 * the first space after 20 characters. If text is less characters, it is left 
 * as is.
 */

require_once "HTMLable.php";

class Article extends HTMLable {
	public $username, $subject, $text, $date, $updated, $latitude, $longtitude, 
	       $aid, $image, $summary;
	public $pos;
	
	public function __construct($art, $summary = false) {
		$this->username = $art['username'];
		$this->subject = $art['subject'];
		$this->text = $art['text'];
		$this->summary = $summary;
		$this->date = $art['date'];
		$this->updated = $art['updated'];
		$this->latitude = $art['latitude'];
		$this->longtitude = $art['longtitude'];
		$this->aid = $art['aid'];
		$this->image = $art['image'];
		
		$this->genHTML();
	}
	
	public function setSummary($bool) {
		// this function only exists to make setting article to summary
		// more explicit
		$this->summary = $bool;
	}
	
	public function genHTML() {
		if ($this->summary)
			$image = "<a href=\"article.php?id=".$this->aid."\"><img src=\"img/pub/".$this->image."s.jpg\" alt=\"\" /></a>";
		else
			$image = "<a href=\"img/pub/".$this->image.".jpg\"><img src=\"img/pub/".$this->image.".jpg\" alt=\"\" /></a>";
	
		$content = $image;
		if ($this->summary)
			$content .= $this->interpretText($this->getFirstLine($this->text));
		else
			$content .= $this->interpretText($this->text);
	
		$this->html = "
		<div class=\"article\">
			<h2>";
		if (strtolower($_SESSION['user']->name) == $this->username)
			$this->html .= "<a href=\"write.php?act=edit&id=".$this->aid."\"><img src=\"img/edit.png\" alt=\"Edit\" title=\"Edit\"></a><a href=\"proc/proc_article.php?act=del&id=".$this->aid."\"><img src=\"img/del.png\" alt=\"Delete\" title=\"Delete\"></a>";
		$this->html .= "
		<a href=\"article.php?id=".$this->aid."\">".$this->subject."</a></h2>
			<h3 class=\"subject\"><span class=\"artUser\">Author: <a href=\"user.php?u=".$this->username."\">".$this->username."</a></span> <span class=\"artDate\">Written: ".$this->date."</span> <span class=\"artUpdated\">last updated: ".$this->updated."</span> <span class=\"artLocation\">location: ".$this->latitude.", ".$this->longtitude."</span></h3>
			<div class=\"text\">".$content;
		
		if ($this->summary) 
			$this->html .= " <a href=\"article.php?id=".$this->aid."\">Lees hier</a>";
		
		$this->html .= "
			</div>
		</div>";
	}
	
	private function getFirstLine($text) {
		$end = strpos($text, ".");
		
		if (!$end && strlen($text) > 20) {
			$end = @strpos($text, " ", 20);
		}
		
		if (!$end)
			return substr($text, 0);
		
		return substr($text, 0, $end+1);
	}
	
	private function interpretText($text = null) {
		if ($text == null)
			$text = $this->text;
		
		$text = stripslashes($text);
		$text = $this->implementNewlines($text);
		$text = $this->implementUrl($text);
		return $text;
	}
	
	private function implementNewlines($text) {
		return str_replace("\n", "<br>", $text);
	}
	
	private function implementUrl($string) {
		$pos = strpos($string, "[url=");
		while ($pos !== false) {
			$end = strpos($string, "[/url]", $pos);
			$old = substr($string, $pos, ($end-$pos)+6);
			$url = substr($old, 5, strpos($old, "]")-5);
			$length = strlen($url);
			$text = substr($old, 6+$length, $end-$pos-6-$length);
			
			$string = str_replace($old, "<a href=\"".$url."\">".$text."</a>", $string);
			$pos = strpos($string, "[url=");
		}
		return $string;
	}
}
?>
